//
//  QRFQScoreProgressView.m
//  TestProgressDemo
//
//  Created by fq on 2020/6/8.
//  Copyright © 2020 fq. All rights reserved.
//

#import "FQScoreProgressView.h"
#define SCREEN_WIDTH UIScreen.mainScreen.bounds.size.width
#define kThemeColor UIColor.orangeColor
@interface FQScoreProgressView (){
    CGFloat layerW;
    CGFloat layerH;
    CGFloat lineWidth;
}

@property (nonatomic, strong) UIImageView* bgIcon;

@property (nonatomic, strong) UIImageView* progressIcon;

@property (nonatomic, strong) UIView *gradientView;

@property(nonatomic,strong)CAShapeLayer *foreLayer;//蒙版layer

@end

@implementation FQScoreProgressView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        layerW = 250 * SCREEN_WIDTH / 360;
        layerH = 237 * layerW / 250.0;
        lineWidth = 33 * layerW / 250.0;

        CGFloat viewX = (frame.size.width - layerW) * 0.5;
        CGFloat viewY = (frame.size.height - layerH) * 0.5;

        self.gradientView.frame = CGRectMake(viewX, viewY, layerW, layerH);
        self.bgIcon.frame = self.gradientView.bounds;
        self.progressIcon.frame = self.gradientView.bounds;

        CGPoint center = CGPointMake(layerW * 0.5, layerH * 0.5+11.1* layerW / 250.0);
        UIBezierPath *bezierPath =[UIBezierPath bezierPathWithArcCenter:center radius:layerW * 0.45 startAngle:2.0/3.0 *M_PI endAngle:1.0/3.0*M_PI clockwise:YES];
        _foreLayer = [CAShapeLayer layer];
        _foreLayer.frame = self.gradientView.bounds;
        _foreLayer.fillColor =[UIColor clearColor].CGColor;
        _foreLayer.lineWidth = lineWidth;
        _foreLayer.strokeColor = kThemeColor.CGColor;
        _foreLayer.strokeEnd = 0.0;
        _foreLayer.lineCap = kCALineCapButt;
        _foreLayer.path = bezierPath.CGPath;
        [self.gradientView.layer addSublayer:_foreLayer];
        self.backgroundColor = UIColor.orangeColor;
        _foreLayer.mask = self.progressIcon.layer;
    }
    return self;
}

-(void)setProgress:(CGFloat)progress
{
    _progress = progress;
    self.foreLayer.strokeEnd = progress;
}


-(UIView *)gradientView
{
    if (!_gradientView) {
        _gradientView = [[UIView alloc]init];
        [self addSubview:_gradientView];
    }
    return _gradientView;
}

-(UIImageView *)bgIcon
{
    if (!_bgIcon) {
        _bgIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ios_score_bg_icon"]];
        _bgIcon.contentMode = UIViewContentModeScaleAspectFit;
        [self.gradientView addSubview:_bgIcon];
    }
    return _bgIcon;
}

-(UIImageView *)progressIcon
{
    if (!_progressIcon) {
        _progressIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ios_score_bg_icon"]];
        _progressIcon.contentMode = UIViewContentModeScaleAspectFit;
        [self.gradientView addSubview:_progressIcon];
    }
    return _progressIcon;
}

@end
